package down.xiaoheihe.work.Controller;

import down.xiaoheihe.work.Entity.TProduct;
import down.xiaoheihe.work.Entity.TProductExample;
import down.xiaoheihe.work.Entity.Timages;
import down.xiaoheihe.work.Entity.TimagesExample;
import down.xiaoheihe.work.Mapper.TOrderMapper;
import down.xiaoheihe.work.Mapper.TProductMapper;
import down.xiaoheihe.work.Mapper.TimagesMapper;
import down.xiaoheihe.work.Mapper.UserMapper;
import down.xiaoheihe.work.Response.ESproduct;
import down.xiaoheihe.work.utils.ElasticUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;

@RestController
@MapperScan("down.xiaoheihe.work.Mapper")
@CrossOrigin
public class 导入数据至ES {
    @Autowired
    TimagesMapper timagesMapper;
    @Autowired
    TProductMapper tProductMapper;
    @Autowired
    ElasticUtils utils;

    @GetMapping("/daorushuju")
    public void upLoadAll() throws IOException {
        List<TProduct> tProducts = tProductMapper.selectByExample(new TProductExample());
        for (TProduct tProduct : tProducts) {
            ESproduct a = new ESproduct(tProduct);
            TimagesExample b = new TimagesExample();
            b.createCriteria().andProductIdEqualTo(tProduct.getId());
            List<Timages> timages = timagesMapper.selectByExample(b);
            a.setTimages(timages);
            utils.创建索引(tProduct,timages);
            utils.更新指定文档(tProduct,timages);
        }
    }
}
